#include <messaginglayer/contactsmodel.h>

#include "contactlistview.h"

ContactListView::ContactListView(QWidget *parent) :
    QListView(parent),
    m_contactDelegate(new ContactDelegate(this))
{
    setSpacing(3);
    setResizeMode(QListView::Adjust);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

    setItemDelegate(m_contactDelegate);
    setDisplayMode(ContactDelegate::LargePicture);

    connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(contactActivated(QModelIndex)));
}

ContactDelegate::DisplayMode ContactListView::displayMode() const
{
    return m_contactDelegate->displayMode();
}

void ContactListView::setDisplayMode(ContactDelegate::DisplayMode mode)
{
    if (mode == ContactDelegate::SmallList
        || mode == ContactDelegate::LargeList) {
        setFlow(QListView::TopToBottom);
        setWrapping(false);
    } else {
        setFlow(QListView::LeftToRight);
        setWrapping(true);
    }
    m_contactDelegate->setDisplayMode(mode);
}

void ContactListView::contactActivated(const QModelIndex &index)
{
    if (index.isValid()) {
        ContactInfo * cinfo = static_cast<ContactInfo *>(index.internalPointer());

        if (cinfo)
            emit messagingRequest(*cinfo);
    }
}
